//
//  CLHTableViewCollectionViewTVCell.m
//  ListViewModel_Example
//
//  Created by 曹连华 on 2019/7/31.
//  Copyright © 2019 clh2046@163.com. All rights reserved.
//

#import "CLHTableViewCollectionViewTVCell.h"
#import "CLHTableViewCollectionViewTVCellModel.h"
#import "CLHCollectionSectionHeaderView.h"
#import "CLHCollectionSectionFooterView.h"
#import <Masonry/Masonry.h>
@interface CLHTableViewCollectionViewTVCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CLHTableViewCollectionViewTVCellModel *rowModel;

@end


@implementation CLHTableViewCollectionViewTVCell

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.itemSize = CGSizeMake(90, 90);
        flowLayout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:NSClassFromString(@"CLHTableViewCollectionViewCVCell") forCellWithReuseIdentifier:@"CLHTableViewCollectionViewCVCell"];
        
        [_collectionView registerClass:NSClassFromString(@"CLHCollectionSectionHeaderView") forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CLHCollectionSectionHeaderView"];
        [_collectionView registerClass:NSClassFromString(@"CLHCollectionSectionFooterView") forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CLHCollectionSectionFooterView"];
        UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
        [_collectionView addGestureRecognizer:tap];
        
    }
    return _collectionView;
}
- (void)initUI {
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.mas_equalTo(0);
        make.trailing.mas_equalTo(-10);
    }];
}

- (void)updateTVCCellWithRowModel:(CLHTableViewCollectionViewTVCellModel *)rowModel atIndexPath:(NSIndexPath *)indexPath {
    self.rowModel = rowModel;
    [self.rowModel.dataSource resetDataSource:self];
    self.collectionView.dataSource = self.rowModel.dataSource;
    [self.collectionView reloadData];
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - CollectionViewCustomDataSource
- (UICollectionReusableView *)collectionViewModel:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath sectionModel:(LVBaseCellSectionModel *)sectionModel {
    LVBaseSectionHeaderFooterModel *headerModel = sectionModel.sectionHeader;
    LVBaseSectionHeaderFooterModel *footerModel = sectionModel.sectionFooter;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        CLHCollectionSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CLHCollectionSectionHeaderView" forIndexPath:indexPath];
        [header updateMainTitle:headerModel.headerFooterTitle subTitle:@"CLHCollectionSectionHeaderView"];
        return header;
        
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        CLHCollectionSectionFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CLHCollectionSectionFooterView" forIndexPath:indexPath];
        [footer updateMainTitle:footerModel.headerFooterTitle subTitle:@"CLHCollectionSectionFooterView"];
        return footer;
        
    }
    return nil;
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    //获取此次点击的坐标，根据坐标获取cell对应的indexPath
    CGPoint point = [longPress locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    //根据长按手势的状态进行处理。
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
            //当没有点击到cell的时候不进行处理
            if (!indexPath) {
                break;
            }
            //开始移动
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
            break;
        case UIGestureRecognizerStateChanged:
            //移动过程中更新位置坐标
            [self.collectionView updateInteractiveMovementTargetPosition:point];
            break;
        case UIGestureRecognizerStateEnded:
            //停止移动调用此方法
            [self.collectionView endInteractiveMovement];
            break;
        default:
            //取消移动
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}
@end
